package com.example.myFirstProject.mvc;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.example.myFirstProject.entity.User;
import com.example.myFirstProject.service.UserService;
import com.example.myFirstProject.utils.MD5Utils;


@Controller
@RequestMapping("/getUsers" )
@SessionAttributes(value={"loginname","password"})
public class UserMvc {

	@Autowired
	private UserService userService;
	
	
	private List<User> list;
	
	private String password;
	
	private int aw;
	
	private User user1;
	
	private ModelAndView modelAndView;
	
	private String loginname;
	
	private String uuid;
	
	@RequestMapping("/insertUser")
	public String insertUser(@ModelAttribute("user")User user){
	    password=user.getPassword();
	    uuid=UUID.randomUUID().toString();
	    password=MD5Utils.Md5(password);
	    user.setUuid(uuid);
	    
	    user.setPassword(password);
	    aw = userService.insertUser(user);
	    return Asd(aw,"/login","/error");
	}
			
	@RequestMapping("/deleteUser/{id}")
	public String deleteUser(@PathVariable("id")int id){
		aw=userService.deleteUser(id);
		return Asd(aw,"/selectUsers","/error");
	}
	
	@RequestMapping("/updateUser")
	public String updateUser(@ModelAttribute("user")User user){
		aw=userService.updateUser(user);
		return Asd(aw,"/updateUser","/error");
	}
	
	@RequestMapping("/selectUser")
	public ModelAndView selectUser(){
		list=new ArrayList<User>();
		list=userService.selectUser();
		modelAndView= new ModelAndView();
		modelAndView.setViewName("selectUsers");
		modelAndView.addObject("list",list);
		return modelAndView;
	}
	
	@RequestMapping("/selectOneUser")
	public ModelAndView selectOneUser(@RequestParam("id")int id){
		user1= new User();
		user1=userService.selectOneUser(id);
		modelAndView= new ModelAndView();
		modelAndView.setViewName("selectOneUser");
        modelAndView.addObject("user1",user1);
        return modelAndView; 
	}
	
	@RequestMapping("/login")
	public String login(@ModelAttribute("user")User user,Model model){
		uuid=userService.selectUUIDAndName(user.getLoginname());
		if(uuid!=null){
			model.addAttribute("noRegister","抱歉,您还没有进行邮箱验证,无法登陆");
			return "/login";
		}else{
			password= MD5Utils.Md5(user.getPassword());
			user.setPassword(password);
			user1=userService.selectNameAndPassword(user);
			if(user1!=null){
				loginname=user1.getLoginname();
				password=user1.getPassword();
				return "/home";
			}
			model.addAttribute("noUser","抱歉,您输入的用户名或者密码错误,请核对登录信息");
			return "/login";
		}
	}
	
	@RequestMapping("/registerUser")
	public ModelAndView updateUUID(@RequestParam("uuid")String uuid){
		
		user1= new User();
		System.out.println("UUID是"+uuid);
		System.out.println("------------------------------");
		
		user1= userService.selectUUID(uuid);
		System.out.println(""+user1.toString());
		System.out.println("------------------------------");
		
		aw=userService.updateUUID(user1);
		modelAndView= new ModelAndView();
		if(aw==1){
			modelAndView.setViewName("selectOneUser");
			modelAndView.addObject("user",user1);
			
			return modelAndView;
			
		}
		modelAndView.setViewName("error");
		modelAndView.addObject("errormes","抱歉,激活邮箱失败");
		return modelAndView;
		
	}
	
	
	@RequestMapping(value="/updatePassword",method=RequestMethod.POST)
	public String updatePassword(@RequestParam("password")String password,Model model){
		aw=userService.updatePassword(password);
		return Asd(aw,"/home","/error");
		
	}
	
	@RequestMapping("/updateName/")
	public String updateName(@ModelAttribute("user")User user){
		aw=userService.updateName(user);
		return Asd(aw,"/home","/error");
	}
	
	@RequestMapping("/selectUid")
	public ModelAndView selectUid(@ModelAttribute("user")User user){
		user1= new User();
		aw=userService.selectUid(user);
		modelAndView= new ModelAndView();
		modelAndView.setViewName("selectOneUser");
		modelAndView.addObject("uid",aw);
		return modelAndView;
		
	}
	
	
	public String Asd(int aw,String args1,String args2){
		if(aw==1){
			return args1;
		}else{
			return args2;
		}
		
	}
	
	/**
	 * 连接页面的方法
	 * 
	 */
	
	@RequestMapping("/")
	public String index(){
		
		return "/index";
	}
	
	@RequestMapping("/insertUser1")
	public String insertUser2(){
		
		return "/insertUser1";
	}
	
	@RequestMapping("/login1")
	public String login(){
		
		return "/login";
	}
	
}

